﻿using System;
using System.Web;
using System.Web.Mvc;
using Elmah;
using Icms.Core;
using ErrorDisplay = Icms.WebUI.Dto.ErrorDisplay;

namespace Icms.WebUI.Controllers
{
    public static class UiExtensions
    {
        public static void Raize(this Exception ex)
        {
            if (HttpContext.Current == null)
                ErrorLog.GetDefault(null).Log(new Error(ex));
            else
                ErrorSignal.FromCurrentContext().Raise(ex);
        }
    }
    public class ErrorController : Controller
    {
        public ActionResult Index(Exception error)
        {
            error.Raize();
            ViewBag.Message = error.Message;
            ViewBag.Inner = error.InnerException != null ? error.InnerException.Message : string.Empty;
            if (Request.IsAjaxRequest())
            {
                if (error is IcmsException)
                    return View("Expectedp");
                return View("Errorp");
            }

            if (error is IcmsException)
                return View("Expected", new ErrorDisplay { Message = error.Message });
            return View("Error", new ErrorDisplay { Message = error.Message });
        }

        public ActionResult HttpError404(Exception error)
        {
            return View();
        }

        public ActionResult HttpError505(Exception error)
        {
            return View();
        }
    }
}